{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [python partial函数](https://blog.csdn.net/qq_33688922/article/details/91890142)\n",
    "Python 提供了一个 functools 的模块，该模块为高阶函数提供支持，partial 就是其中的一个函数，该函数的形式如下：\n",
    "```py\n",
    "functools.partial(func[,*args][, **kwargs])\n",
    "```\n",
    "假设有如下函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T12:01:42.500675Z",
     "start_time": "2020-08-26T12:01:42.489212Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "8\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "def multiply(x, y):\n",
    "    return x * y\n",
    "\n",
    "#现在，我们想返回某个数的双倍，即\n",
    "print(multiply(3, y=2))\n",
    "print(multiply(4, y=2))\n",
    "print(multiply(5, y=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的调用有点繁琐，每次都要传入 y=2，我们想到可以定义一个新的函数，把 y=2 作为默认值，即："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T12:02:04.836612Z",
     "start_time": "2020-08-26T12:02:04.828077Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "8\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "def double(x, y=2):\n",
    "    return multiply(x, y)\n",
    "\n",
    "#现在，我们可以这样调用了\n",
    "print(double(3))\n",
    "print(double(4))\n",
    "print(double(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "事实上，我们可以不用自己定义 double，利用 partial，我们可以这样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T12:02:28.470571Z",
     "start_time": "2020-08-26T12:02:28.461974Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "8\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "from functools import partial\n",
    "\n",
    "def multiply(x, y):\n",
    "    return x * y\n",
    "\n",
    "double = partial(multiply, y=2)\n",
    "\n",
    "print(double(3))\n",
    "print(double(4))\n",
    "print(double(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "partial 接收函数 multiply 作为参数，固定 multiply 的参数 y=2，并返回一个新的函数给 double，这跟我们自己定义 double 函数的效果是一样的。所以，简单而言，partial 函数的功能就是：把一个函数的某些参数给固定住，返回一个新的函数。需要注意的是，我们上面是固定了 multiply 的关键字参数 y=2，如果直接使用：\n",
    "```py\n",
    "double = partial(multiply, 2)\n",
    "```\n",
    "则 2 是赋给了 multiply 最左边的参数 x，不信？我们可以验证一下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T12:04:22.603069Z",
     "start_time": "2020-08-26T12:04:22.597962Z"
    }
   },
   "outputs": [],
   "source": [
    "from functools import partial\n",
    " \n",
    "def subtraction(x, y):\n",
    "    return x - y\n",
    " \n",
    "f = partial(subtraction, 4)  # 4 赋给了 x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-26T12:04:35.143771Z",
     "start_time": "2020-08-26T12:04:35.137295Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-6"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
